package com.mabeijianxi.smallvideo;

import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.util.List;

import mabeijianxi.camera.MediaRecorderActivity;
import mabeijianxi.camera.VCamera;
import mabeijianxi.camera.model.MediaRecorderConfig;
import mabeijianxi.camera.util.DeviceUtils;

import static android.hardware.Camera.open;

public class MainActivity extends AppCompatActivity {

    private int[] mInts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       //mInts = initCamera();
        initSmallVideo(this);
    }

    public void go(View c) {


        MediaRecorderConfig config = new MediaRecorderConfig.Buidler()
                .doH264Compress(true)
                .smallVideoWidth(480)
                .smallVideoHeight(480)
                .recordTimeMax(10 * 1000)
                .maxFrameRate(10)
                .minFrameRate(8)
                .captureThumbnailsTime(1)
                .recordTimeMin((int) (0.5 * 1000))
                .build();
        MediaRecorderActivity.goSmallVideoRecorder(this, VideoPlayerActivity.class.getName(), config);
    }

    private int[] initCamera() {
        Camera mCamera = Camera.open();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();// 得到摄像头的个数
        int [] s = new int[]{};
        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);// 得到每一个摄像头的信息
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {// 代表摄像头的方位，CAMERA_FACING_FRONT前置
                // CAMERA_FACING_BACK后置

                // 打开当前选中的摄像头
                mCamera = open(i);
                Camera.Parameters parameters = mCamera.getParameters();
                List<Camera.Size> videoSizes =
                        parameters.getSupportedVideoSizes();
                for (int x = 0; x < videoSizes.size(); x++) {
                    Camera.Size size = videoSizes.get(x);
                    Log.i("MainActivity", "initCamera: 摄像头支持的尺寸"+"\tsize.height\t"+size.height+"\tsize.width\t"+size.width);
                }

              s = new int[]{videoSizes.get(1).width, videoSizes.get(1).height};
                break;
            }
        }

        mCamera.release();
        return s ;
    }

    public static void initSmallVideo(Context context) {
        // 设置拍摄视频缓存路径
        File dcim = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        if (DeviceUtils.isZte()) {
            if (dcim.exists()) {
                VCamera.setVideoCachePath(dcim + "/mabeijianxi/");
            } else {
                VCamera.setVideoCachePath(dcim.getPath().replace("/sdcard/",
                        "/sdcard-ext/")
                        + "/mabeijianxi/");
            }
        } else {
            VCamera.setVideoCachePath(dcim + "/mabeijianxi/");
        }
        // 开启log输出,ffmpeg输出到logcat
        VCamera.setDebugMode(true);
        // 初始化拍摄SDK，必须
        VCamera.initialize(context);
    }
}
